#!/usr/bin/python

#a script, that one can use to open programms in background or 
#open a file with a programm which u specify in source file list
#the script searches by the ending of the file in the source-file
#every line in the source-file looks like this:
#	<ending(without ".")>#<specific programm>#<"nobg"|just nothing>
#if there is no specific ending, the file will be opened by vim

import sys
import os

class Open:
	
	bg = "&"
	fileTyp = "noType"
	programm = "noPro"
	source = "/home/stefan/Coding/open/source"

#--------------------constructor--------------------------#
	def __init__(self, fileToOpen, justExec, arglist):
			self.fileToOpen = fileToOpen
			self.justExec = justExec
			self.arglist = arglist
	
#function that is called first, when a file or a programm should be opened
	def call(self):
		#checks if a regular file should be executed or a programm should be found to open	
			if self.justExec == 1:
				#file should be executed
					temp = "./" + self.fileToOpen
					self.fileToOpen = temp
					del self.arglist[0]
					del self.arglist[0]
			else:
				#a programm should be find to open the file or given is already a programm
					self.getTyp()
					self.getPro()
			
			self.mexec()

#function that searches for a specific ending of a file, e.g. ".pdf"	
	def getTyp(self):
			parts = self.fileToOpen.rpartition('.')
			if parts[2] != self.fileToOpen:
							self.fileTyp = parts[2]
								
#function that looks in the source file for a fiting programm for the file
#(by the specific ending)
#if there is no ending vim is set as programm to use to open
	def getPro(self):
			if self.fileTyp != "noType":
					liste = open (self.source,"r+")
					for line in liste:
						parts = line.split('#')
						if parts[0] != line:
							#regular line in path
								if parts[0] == self.fileTyp:
									#hit in the table
										if len(parts) == 3:
											#ensure there are 3 elements in the line of sourcefolder
												if parts[2][:-1] == "nobg":
														self.bg =""
										self.programm = parts[1]
										break
			else:
				#file has no specific ending, but is a regular file (not sth like a programm)
					if os.path.isfile(self.fileToOpen):
							self.programm = "vim"
							self.fileTyp = "regFile"
							self.bg = ""
	
	def mexec(self):
			if self.justExec == 1:
					os.execv(self.fileToOpen, self.arglist)
			else:
					if (self.programm == "noPro") & (self.fileTyp != "noType"):
							print "there is no entry in the source file for this type of file"
							exit(1)
					elif self.fileTyp == "noType":
							full = self.fileToOpen+self.bg
						
					else:
							full = self.programm+" "+self.fileToOpen+self.bg
					os.system(full)

if sys.argv[1] == "-help":
		print ">>>>for opening a file call like this: ./open <file> "
		print ">>>>for executing an executable file or a programm use: ./open -exec <file/programm>"
elif sys.argv[1] == "-exec":
		runner = Open(sys.argv[2], 1, sys.argv)
		runner.call()
else:
		runner = Open(sys.argv[1], 0, sys.argv)
		runner.call()

